<?php
/**
 * Student Sibling Controller
 *
 * Student Sibling Information Management
 * student entry
 *
 * @version $Id: SiblingController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package student
 * @subpackage Controller
 */
class Student_SiblingController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $arrayData = $ini->get('array_data')->toArray();
        $this->view->callingArr = $arrayData['sibling'];
        $this->view->callingJSON = Zend_Json::encode($arrayData['sibling']);
    }

    public function indexAction()
    {
        if ($this->getRequest()->getParam('year_semester')){
       	    $this->view->year_semester = $this->getRequest()->getParam('year_semester');
       	    $this->view->class_id = $this->getRequest()->getParam('class_id');
       	}
       	else{
       	    $cookie = Zend_Controller_Request_Http::getCookie();
       	    if ( isset($cookie['Student_year_semester'])) {
       	        $this->view->year_semester = $cookie['Student_year_semester'];
       	        $this->view->class_id = $cookie['Student_class_id'];
       	    }else {
       	        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
       	        $this->view->class_id = $this->view->lowYear.'-1';
       	    }
       	}
    }

    public function setAction()
    {
        $request = $this->getRequest();
        $student_sn = $request->getParam('student_sn');
        $name = $request->getParam('name');
        $calling = $request->getParam('calling');
        $born_year = $request->getParam('born_year');
        $silbing = new Student_Model_StudentSibling();
        $form = new Student_Form_StudentSibling();
        $data = array();
        $message = array();
        if ($name){
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['calling'] = $calling[$sn];
                $data['born_year'] = $born_year[$sn];
                $data['student_sn'] = $student_sn;
                $data['sn'] = $sn;
                if ($form->isValid($data)){
                    $silbing->assignIdentifier($sn);
                    $silbing->fromArray($data);
                    $silbing->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }
        // new data

        $name = $request->getParam('new_name');
        $calling = $request->getParam('new_calling');
        $born_year = $request->getParam('new_born_year');
        if ($name) {
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['calling'] = $calling[$sn];
                $data['born_year'] = $born_year[$sn];
                $data['student_sn'] = $student_sn;

                if ($form->isValid($data)){
                    $silbing = new Student_Model_StudentSibling();
                    $silbing->fromArray($data);
                    $silbing->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }

        if ($message)
        $this->_helper->json(array('error'=>$message));
        else
        $this->_helper->json(array('success'=>1));
    }

    public function getdataAction()
    {
        $this->getHelper('layout')->disableLayout();
        $sn = (int)$this->getRequest()->getParam('sn');
        $studentSibing = Doctrine_Core::getTable('Student_Model_StudentSibling');
        $s = $studentSibing->findBy('student_sn',$sn);
        $res = $s->toArray();
        $this->view->data = $res;
    }

    public function deleteAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn',0);
        $q = Doctrine_Query::create()
        ->delete('Student_Model_StudentSibling')
        ->where('sn=?',$sn);
        $q->execute();

        $this->_helper->json(1);
    }

}

